Skip to content

bpo-37064: add option -k to Tools/scripts/pathfix.py#15548

Merged
vstinner merged 8 commits into
python:masterfrom
PatrikKopkan:k-option
Sep 5, 2019
Merged

bpo-37064: add option -k to Tools/scripts/pathfix.py#15548
vstinner merged 8 commits into
python:masterfrom
PatrikKopkan:k-option

Conversation

@PatrikKopkan

@PatrikKopkan PatrikKopkan commented Aug 27, 2019

Copy link
Copy Markdown
Contributor
  • with this option it is possible to keep flags when changing shebang line

https://bugs.python.org/issue37064

 - with this option it is possible to keep flags
when changing shebang line
Comment thread Tools/scripts/pathfix.py Outdated
new_interpreter = None
preserve_timestamps = False
create_backup = True
add_flag = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable looks unused.

Comment thread Tools/scripts/pathfix.py Outdated

def parse_shebang(shebangline):
end = len(shebangline)
start = shebangline.find(b' -', 0, end) # .find() returns index at space

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the comment, IMHO it's not really helpful.

", 0, end" are the default value and so can be removed.

Comment thread Tools/scripts/pathfix.py Outdated


def parse_shebang(shebangline):
end = len(shebangline)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that on Linux, the first whitespace (U+0020) indicates the beginning of options.

Comment thread Tools/scripts/pathfix.py Outdated
start = shebangline.find(b' -', 0, end) # .find() returns index at space
if start == -1:
return b'', b''
print(shebangline[(start + 2):end])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit debug print please (remove it).

Comment thread Tools/scripts/pathfix.py Outdated


def fixline(line):
global keep_flags

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this global statement is useful. Variables are global by default.

Comment thread Tools/scripts/pathfix.py Outdated
if keep_flags:
flags = parse_shebang(line)
return b'#! ' + new_interpreter + b" -" + flags
return b'#! ' + new_interpreter + b'\n'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have:

    if keep_flags:
        flags = parse_shebang(line)
    else:
        flags = b''
    return b'#! ' + new_interpreter + flags + b'\n'

So parse_shebang() must remove the trailing newline.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have zero idea how I deleted. In editor I still have:
return b'#! ' + new_interpreter + b' -' + flags + b'\n'

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
subprocess.call([sys.executable, self.script, '-i', '/usr/bin/python', '-n', self.temp_file])
with open(self.temp_file) as f:
output = f.read()
self.assertEqual(output, '#! /usr/bin/python\n' + 'print("Hello world")\n')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test using -k when the shebang does not contain any flag.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
self.temp_file = support.TESTFN

with open(self.temp_file, 'w') as f:
f.write('#! /usr/bin/env python -R\n' + 'print("Hello world")\n')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have an helper method which takes two arguments: (shebang, pathfix_flags) and return the new shebang. To get tests which look like:

def test_keep_flags(self):
  shebang = '#! /usr/bin/env python -R'
  self.assertEqual(
    self.pathfix(shebang, ['-i', '/usr/bin/python']),
    '#! /usr/bin/python')
  self.assertEqual(
    self.pathfix(shebang, ['-i', '/usr/bin/python', '-k']),
    '#! /usr/bin/python -R')

Comment thread Lib/test/test_tools/test_pathfix.py Outdated

@classmethod
def setupUpClass(cls):
self.addCleanup(support.unlink, support.TESTFN)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work: self doesn't exist. You may use setUp().

@@ -0,0 +1 @@
Add flag -k to pathscript.py. This flag preserve flags from shebang before change and adds them after change. No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest: "Add flag -k to pathscript.py script: preserve shebang flags."

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
@@ -0,0 +1,48 @@
import unittest, os, subprocess

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put one import per line: see PEP 8.

Try also to sort imports.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
subprocess.call([sys.executable, self.script, *pathfix_flags, self.temp_file])
with open(self.temp_file) as f:
output = f.read()
print(output)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove debug print when you submit a PR.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
self.addCleanup(support.unlink, support.TESTFN)

def pathfix(self, shebang, pathfix_flags):
with open(self.temp_file, 'w') as f:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to specify the encoding: pass encoding="utf8" to this open, and the other one below.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python', '-k', '-n']),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove -n from tests and always put it in pathfix() function.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python', '-k', '-n']),
'#! /usr/bin/python -R\n' + 'print("Hello world")\n'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it's useful to check for 'print("Hello world")\n' in each test. I would prefer to test it in pathfix() and just return the first line.

Something like this in pathfix():

lines = output.split('\n')
self.assertEqual(lines[1:], ['print("Hello world")\n']
shebang = lines[0][:-1] # strip the newline
return shebang

Comment thread Lib/test/test_tools/test_pathfix.py

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the code now looks way better! A few more comments.

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
shebang = lines[0][:-1] # strip the newline
return shebang

def test_pathfix_keeping_flags(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer:

  • put test_pathfix_without_keeping_flags() code into test_pathfix()
  • move test_pathfix_keeping_flags() after test_pathfix()
  • add a test in test_pathfix_keeping_flags(): use -k but the shebang doesn't contain any flag (the bug that you just fixed)

Comment thread Tools/scripts/pathfix.py Outdated
def parse_shebang(shebangline):
shebangline = shebangline.strip()
end = len(shebangline)
start = shebangline.find(b' -', 0, end)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"end" argument is useless: shebangline.find(b' -') behaves the same and is simpler. Remove 0 and end.

Comment thread Tools/scripts/pathfix.py Outdated
if keep_flags:
flags = parse_shebang(line)
return b'#! ' + new_interpreter + b" -" + flags
return b'#! ' + new_interpreter + b'\n'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vstinner
vstinner merged commit 50254ac into python:master Sep 5, 2019
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
Add flag -k to pathscript.py script: preserve shebang flags.
vstinner added a commit that referenced this pull request Sep 25, 2019
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548)

Add flag -k to pathscript.py script: preserve shebang flags.

(cherry picked from commit 50254ac)

* bpo-37064: Add option -a to pathfix.py tool (GH-15717)

Add option -a to Tools/Scripts/pathfix.py script: add flags.

(cherry picked from commit 1dc1acb)
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Add flag -k to pathscript.py script: preserve shebang flags.
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
Add flag -k to pathscript.py script: preserve shebang flags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants